package com.xinchuang.mapper.targetSystem;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xinchuang.entity.targetSystem.external.Blow;
import com.xinchuang.entity.targetSystem.system.SystemsRelation;
import com.xinchuang.entity.targetSystem.target.TargetData;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zkwczx
 * @ClassName SystemsRelation
 * @Description: 体系关联表
 * @data 2021/3/30  17:43
 * @Version V1.0
 */
public interface SystemsRelationMapper extends BaseMapper<SystemsRelation> {

    @Insert(value = {
            "INSERT INTO systems_relation ",
            "(sys_rel_id,sys_id,target_id,target_type) ",
            "VALUES ",
            "(#{sysRelId},#{sysId},#{targetId},#{targetType})",
    })
    int save(SystemsRelation systemsRelation);

    @Delete(value = "DELETE FROM systems_relation WHERE sys_id = #{sysId}")
    int deleteBySysId(String sysId);

    @Select(value = {
            "SELECT sys_rel_id,sys_id,target_id,target_type FROM systems_relation WHERE sys_id = #{sysId} "
    })
    @Results(id = "commonResultMap", value = {
            @Result(property = "sysRelId", column = "sys_rel_id"),
            @Result(property = "sysId", column = "sys_id"),
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "targetType", column = "target_type")
    })
    List<SystemsRelation> findById(String sysId);

    /**
     * 查询系统中包含的目标id
     *
     * @return
     */
    @Select(value = {
            "<script>",
            "SELECT target_id FROM system_relation WHERE s_id IN (",
            "<foreach collection='systemList' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>",
            ")",
            "</script>",
    })
    List<String> selectRelationTargetNameBySystemList(@Param("systemList") List<String> systemList);

    /**
     * 查询不包含系统的中的其他目标
     *
     * @return
     */
    @Select(value = {
            "<script>",
            "SELECT b.target_id AS targetId,b.target_name AS targetName,b.target_classify AS targetClassify,b.lon,b.lat,b.height FROM ",
            "(SELECT target_id FROM systems_relation WHERE sys_id = #{sysId} AND target_type = '1') AS a ",
            "LEFT JOIN ",
            "(SELECT target_id,target_name,target_classify,lon,lat,height FROM target_base_data) AS b ",
            "ON ",
            "a.target_id = b.target_id",
            "</script>",
    })
    List<Map<String, Object>> selectNoSystemTargetList(@Param("sysId") String sysId);

    /**
     * 查询不包含系统的中的其他目标
     *
     * @return
     */
    @Select(value = {
            "<script>",
            "SELECT a.target_id,a.target_name,a.lon,a.lat,a.height,a.target_classify FROM ",
            "(SELECT target_id,target_name,target_classify,lon,lat,height FROM target_base_data WHERE delete_sign = 1) AS a ",
            "JOIN ",
            "(SELECT target_id FROM systems_relation WHERE sys_id = #{sysId} AND target_type = #{targetType}) AS b ",
            "ON ",
            "a.target_id = b.target_id",
            "</script>",
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "targetName", column = "target_name"),
            @Result(property = "targetClassify", column = "target_classify")
    })
    List<TargetData> selectSystemTargetAll(@Param("sysId") String sysId, @Param("targetType") String targetType);


    /**
     * @return java.util.List<java.lang.String>
     * @author zhangxuan
     * @Description TODO 查询体系包含的目标
     * @Date 16:43 2022/4/1
     * @Param [sysId]
     **/
    @Select("SELECT target_id FROM systems_relation WHERE sys_id = #{sysId}")
    List<String> selectSystemsTargetId(@Param("sysId") String sysId);

    /**
     * @return java.util.List<java.lang.String>
     * @author zhangxuan
     * @Description TODO 查询体系包含的目标
     * @Date 16:43 2022/4/1
     * @Param [sysId]
     **/
    @Select("SELECT target_id FROM systems_relation WHERE sys_id = #{sysId} AND target_type = #{targetType}")
    List<String> selectSystemsRelation(@Param("sysId") String sysId, @Param("targetType") String targetType);

    /**
     * 查询体系关联的打击评估信息
     *
     * @param sysId
     * @return
     */
    @Select(value = "SELECT target_id,message FROM blow_levels WHERE sys_id = #{sysId} LIMIT 1")
    @Results(value = {
            @Result(property = "targetId", column = "target_id")
    })
    Blow getBlow(@Param("sysId") String sysId);

    /**
     * 新增系统关联
     */
    @Select(value = {
            "<script>",
            "SELECT target_id FROM systems_relation WHERE sys_id = #{sysId} AND target_id NOT IN (",
            "<foreach collection='set' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>",
            ")",
            "</script>"
    })
    List<String> selectRelationNoTargets(String sysId, Set set);

    @Select(value = {
            "SELECT sr.target_id,(SELECT target_name FROM target_base_data WHERE target_id = sr.target_id ) AS target_name",
            "FROM systems_relation sr",
            "WHERE sr.sys_id = #{sysId} AND sr.target_type = 1"
    })
    List<SystemsRelation> selectRelationTargetPull(String sysId);

}

